﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Znice
{
    public partial class ZEditor : UserControl
    {
        public ZEditor()
        {
            InitializeComponent();
        }

        #region 开放属性

        private ToolbarMode toolbarMode;
        /// <summary>
        /// 按钮菜单配置
        /// <para>当配置为Customer时候，必须给 ToolBarCustomer 赋值</para>
        /// </summary>
        [DefaultValue(ToolbarMode.Default), Category("Znice"), Description("编辑器工具栏菜单")]
        public ToolbarMode ToolBarMenu
        {
            get => toolbarMode;
            set
            {
                toolbarMode = value;
                ResetTooBars();
                Invalidate();
            }
        }

        private string toolBarCustomer = "Font,Bold,Italic,FontPlusSize,FontMinusSize,StrikeThrough,UnderLine,SubScript,SuperScript,Table,BackgroundColor,ForeColor,AlignLeft,AlignCenter,AlignRight,UnorderedList,Indent,Outdent,Link,Picture,Print,Undo,Redo,Delete,Open,SaveAs";
        /// <summary>
        /// 自定义工具栏按钮
        /// <para>仅当编辑器工具栏菜单配置为Customer时生效，多个值用,分开</para>
        /// </summary>
        [Category("Znice"), Description("仅当编辑器工具栏菜单配置为Customer时生效，多个值用,分开")]
        public string ToolBarCustomer
        {
            get => toolBarCustomer;
            set
            {
                if (toolbarMode != ToolbarMode.Customer)
                {
                    return;
                }
                toolBarCustomer = value;
                ResetTooBars();
                Invalidate();
            }
        }

        private Color toolBarBackColor = SystemColors.Control;
        /// <summary>
        /// 工具栏菜单背景色
        /// </summary>
        [Category("Znice"), Description("工具栏菜单背景色")]
        public Color ToolBarBackColor
        {
            get=> toolBarBackColor;
            set
            {
                toolBarBackColor = value;
                toolBarStrip.BackColor = toolBarBackColor;
                Invalidate();
            }
        }

        private Color richTextBoxBackColor = SystemColors.Window;
        /// <summary>
        /// 编辑器文本框背景色
        /// </summary>
        [Category("Znice"), Description("编辑器文本框背景色")]
        public Color RichTextBoxBackColor
        {
            get => richTextBoxBackColor;
            set
            {
                richTextBoxBackColor = value;
                richTextBox.BackColor = richTextBoxBackColor;
                Invalidate();
            }
        }

        /// <summary>
        /// 获取或设置编辑器文本值
        /// </summary>
        [Category("Znice"), Description("编辑器文本值")]
        public byte[]? RichText
        {
            get
            {
                if (richTextBox.Text.Trim().Length > 0)
                {
                    MemoryStream mstream = new();
                    richTextBox.SaveFile(mstream, RichTextBoxStreamType.RichText);
                    //将流转换成数组
                    return mstream.ToArray();
                }
                return null;
            }
            set
            {
                if (value != null)
                {
                    byte[] bWrite = value;
                    //将数组转换成stream
                    MemoryStream mstream = new(bWrite, false);
                    //将stream填充到RichTextBox
                    richTextBox.LoadFile(mstream, RichTextBoxStreamType.RichText);
                }
            }
        }

        #endregion

        /// <summary>
        /// 获取所有工具栏
        /// </summary>
        /// <returns></returns>
        private IList<string> GetToolBarFullName()
        {
            var dic = new List<string>();
            var fieldinfos = typeof(ToolBar).GetFields();
            foreach (var field in fieldinfos)
            {
                if (field.FieldType.IsEnum)
                {
                    dic.Add(field.Name);
                }
            }
            return dic;
        }

        /// <summary>
        /// 设置工具栏按钮
        /// </summary>
        private void ResetTooBars()
        {
            IList<string> toolBars = new List<string>();
            if (toolbarMode == ToolbarMode.Default)
            {
                toolBars = new List<string> { "Font", "Bold", "Italic", "PlusFontSize", "MinusFontSize", "UnderLine", "BackgroundColor", "ForeColor", "AlignLeft", "AlignCenter", "AlignRight", "Picture", "Undo", "Redo" };
            }
            else if (toolbarMode == ToolbarMode.Simple)
            {
                toolBars = new List<string> { "Font", "Bold", "Italic", "BackgroundColor", "ForeColor" };
            }
            else if (toolbarMode == ToolbarMode.Full)
            {
                toolBars = GetToolBarFullName();
            }
            else {
                var fullToolBar = GetToolBarFullName();
                string[] cusToolBar = toolBarCustomer.Split(',');
                foreach (string s in cusToolBar)
                {
                    if (fullToolBar.Contains(s))
                    {
                        toolBars.Add(s);
                    }
                }
            }
            foreach (var toolStripItem in toolBarStrip.Items)
            {
                if (toolStripItem is ToolStripButton button)
                {                    
                    button.Visible = toolBars.Contains((string)button.Tag);
                }
            }
        }

        private void ZEditor_Load(object sender, EventArgs e)
        {
            ResetTooBars();
        }

        private void ToolStripButton_Click(object sender, EventArgs e)
        {
            ITextFormat txtFomat = TextFormatFactory.CreateTextFormat((sender as ToolStripButton)?.Tag.ToString());
            txtFomat.SetTextFormat(richTextBox);
        }
    }
}
